package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page=setmealMapper.pageList(setmealPageQueryDTO);


        return new PageResult(page.getTotal(),page.getResult());
    }
@Transactional
    @Override
    public void addSermealOrDish(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.add(setmeal);

    List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
    setmealDishes.forEach(e-> e.setSetmealId(setmeal.getId()));

    if (setmealDishes.size()> 0 &&setmealDishes !=null){

setmealDishMapper.add(setmealDishes);
    }
}

    @Override
    public void openOrClose(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.SetmealUpDate(setmeal);

    }
@Transactional
    @Override
    public SetmealVO byId(Long id) {


       SetmealVO setmealVO= setmealMapper.byid(id);

       List<SetmealDish> list =setmealDishMapper.byid(id);
       setmealVO.setSetmealDishes(list);

        return setmealVO;
    }
@Transactional
    @Override
    public void updateSetmealDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.SetmealUpDate(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        if (setmealDishes.size()>0 && setmealDishes !=null){
            setmealDishMapper.deleteDish(setmealDTO.getId());
            setmealDishMapper.add(setmealDishes);
        }
    }
@Transactional
@Override
    public void deleteSetmeal(Integer[] ids) {
        setmealMapper.deleteByid(ids);
        setmealDishMapper.deleteSetmealDish(ids);


    }

    @Override
    public List<Setmeal> byCategoryID(Long categoryId) {
      List<Setmeal> list= setmealMapper.byCategoryID(categoryId);
      return list;

    }


}
